using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using DevExpress.XtraEditors;
using Pharmacy.Data.Entity;



namespace PharmacyPharnet.Core
{
	/// <summary>
	/// Summary description for DialogViewBase.
	/// </summary>
	/// <author></author>
	/// <history>
	/// 2011.01.26 - ThangTran: refactored code to use updated IView.
	/// </history>
    public class DialogViewBase : XtraForm, IView
	{
        
        bool _isOnLoadErrorEventHandled = false;

        /// <summary>
        /// Gets or sets value to indicate that the event LoadError has been handled.
        /// </summary>
        // Author: hungle@dicentral.com - 2011.01.12
        public virtual bool IsOnLoadErrorEventHandled
        {
            get { return _isOnLoadErrorEventHandled; }
            set { _isOnLoadErrorEventHandled = value; }
        }
        public void RaiseEventLoadError()
        {
            if (LoadError != null)
            {
                LoadError(this, null);
            }
        }
        public event EventHandler LoadError;

        private ControlViewBase _ControlViewBase;
        public ControlViewBase ControlViewBase
        {
            get { return _ControlViewBase; }
            set { _ControlViewBase = value; }
        }        

		#region Windows Form Designer generated code     

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        
        public DialogViewBase()
        {
            InitializeComponent();
            
        }

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DialogViewBase));
            this.SuspendLayout();
            // 
            // DialogViewBase
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(192, 142);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.HelpButton = true;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "DialogViewBase";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "DialogViewBase";
            this.ResumeLayout(false);

		}
		#endregion
        
        protected virtual void OnLoadError(EventArgs e){
            if(LoadError != null){
                LoadError(this, e);
            }
        }

        /// <summary>
        /// Fired when the View is loaded.
        /// </summary>
		protected override void OnLoad(EventArgs e)
		{
			try
			{
                // TODO: Add any initialization after the InitForm call
                InitializeBO();
				PopulateStaticData();
				PopulateData();				
				SetFocusedIndex();

                // automatically apply user permission
                ApplyUserPermissionOnToolBar();

                // Apply localize
                LocalizerView.ApplyLanguage(this);

                base.OnLoad(e);
			}
			catch(Exception ex)
			{
                OnLoadError(e);

			    //NotifyUserUtil.ShowException(ex);
			}            
		}
        /// <summary>
        /// vinhvo : 2011-01-20
        /// Add Event LoadChildFunction Completed
        /// </summary>
        public event EventHandler LoadChildFunctionCompleted;
        public void AlertLoadChildFunctionCompleted(IList childFunction)
        {
            if (LoadChildFunctionCompleted != null)
                LoadChildFunctionCompleted(childFunction, null);
        }
		/// <summary>
		/// Populates the data.
		/// </summary>
		protected virtual void PopulateData()
		{
		}
		/// <summary>
		/// Populates the data for filter control or the editing control.
		/// </summary>
		protected virtual void PopulateStaticData()
		{
		}

		/// <summary>
		/// Initializes the Business object for the form.
		/// </summary>
		protected virtual void InitializeBO()
		{
		}
		/// <summary>
		/// Set default focused index of UI control
		/// </summary>
		protected virtual void SetFocusedIndex()
		{

		}

        #region IView Members

        /// <summary>
        /// Gets or sets feature name of this View.
        /// </summary>
	    private string _featureTypeName;

        /// <summary>
        /// Gets or sets feature name of this View.
        /// </summary>
        public string FeatureTypeName
        {
            get
            {
                return _featureTypeName ?? (_featureTypeName = this.GetType().FullName);
            }
            set
            {
                // --- Begin parameters checking code -----------------------------
                Debug.Assert(value != null);  // this field can't accept null value
                // --- End parameters checking code -------------------------------

                _featureTypeName = value;
            }
        }

	    /// <summary>
	    /// Gets a value that indicates whether this View is closed or not.
	    /// </summary>
        public bool IsClosed { get; set; }

	    /// <summary>
	    /// Gets a value that indicates wheter the event is prevented or not.
	    /// </summary>
        public bool EventIsPrevented { get; protected set; }

	    /// <summary>
        /// Applies current logging in user's permission to all toolbars on this View.
	    /// </summary>
	    public void ApplyUserPermissionOnToolBar()
	    {
            // call extension method to avoid duplicating code
	        ViewHelper.ApplyUserPermissionOnToolBar(this);
	    }

	    #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="functionName"></param>
        /// <param name="functions"></param>
        /// <retuns></returns>
        /// <author></author>
        /// <history>
        /// 2011.01.25 - ThangTran: copied code from ControlViewBase to quick-fix the Enable/Disable menubar items issue.
        /// </history>
        private bool CheckIsEnable(string functionName, List<Function> functions)
        {
            bool isEnable = false;
            foreach (Function function in functions)
            {
                if (function.Name == functionName)
                {
                    isEnable = true;
                    break;
                }
            }

            return isEnable;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="functionName"></param>
        /// <param name="functions"></param>
        /// <returns></returns>
        /// <author></author>
        /// <history>
        /// 2011.01.25 - ThangTran: copied code from ControlViewBase to quick-fix the Enable/Disable menubar items issue.
        /// </history>
        private bool CheckIsVisible(string functionName, List<Function> functions)
        {
            bool isVisible = false;
            foreach (Function function in functions)
            {
                if (function.Name == functionName)
                {
                    isVisible = function.IsVisible.GetValueOrDefault();
                    break;
                }
            }

            return isVisible;
        }
    }
}

